package com.test.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.entity.CardType;
import com.test.entity.OrderDetail;
import com.test.entity.Orders;

public class OrderDetailDAO extends HibernateDaoSupport {

	public List<Object[]> queryBySeleQuantity() {
		String hql = "select  od.cardType.id,count(od.quantity)  from OrderDetail as od  group by od.cardType order by count(od.quantity) desc";
		List<Object[]> list = this.getHibernateTemplate().find(hql);
		for (int i = 0; i < list.size(); i++) {
			int id = (Integer) (list.get(i)[0]);
			CardType ct = this.getHibernateTemplate().get(CardType.class, id);
			list.get(i)[0] = ct;
		}
		return list;
	}

	public List<Orders> queryorderdetail(String noString) {
		List<Orders> list = this.getHibernateTemplate().find(
				"from OrderDetail where id=?", noString);
		return list;
	}

	public void saveOrdersdetail(OrderDetail orderDetail) {
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		Transaction tr = session.beginTransaction();
		session.save(orderDetail);
		tr.commit();
		session.close();
	}

	public List<OrderDetail> queryDetails(String orderId) {
		String hql = "from OrderDetail as model where model.orders.orderId=?";
		List<OrderDetail> list = this.getHibernateTemplate().find(hql, orderId);
		return list;
	}
}
